package com.jcl.recruit.api.user.controller;

import com.jcl.core.resp.ApiResponse;
import com.jcl.recruit.user.model.UserOtherjob;
import com.jcl.recruit.user.service.UserOtherjobService;
import com.jcl.recruit.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 刘明新
 * @date 2018/8/2 下午7:12
 */
@RestController
@RequestMapping("/api/user")
public class UserApiController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserOtherjobService userOtherjobService;

    // 爬虫的数据新增库
    @RequestMapping(value = "/setotherjobrela",method = RequestMethod.POST)
    public ApiResponse setotherjobrela(@RequestBody Map<String,String> entity){

        if(org.apache.commons.lang3.StringUtils.isNotBlank(entity.get("other_jids")) && StringUtils.isNotBlank(entity.get("user_email"))){
            Integer id = userService.selectPrimaryIdByEmail(entity.get("email"));
            String[] jids = entity.get("other_jids").split(",");
            List<UserOtherjob> userOtherjobs = new ArrayList<>();
            for(int i=0;i<jids.length;i++){
                UserOtherjob userOtherjob = new UserOtherjob();
                userOtherjob.setOtherJid(jids[i]);
                userOtherjob.setUserId(id);
                userOtherjobs.add(userOtherjob);
            }
            if(userOtherjobs.size() > 0) {
                userOtherjobService.batchInsert(userOtherjobs);
            }
        }else{
            return ApiResponse.error("other_jids不能为空");
        }
        return ApiResponse.success("操作成功");
    }


}
